package com.geo.fight;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;



public class Canvas extends JPanel implements ActionListener{
	
	/**
	 * @author Geo
	 */
	private static final long serialVersionUID = 1L;
	private Timer timer;
	private Timer timer2;
	private Gunner gunner = new Gunner();
	private ArrayList<Ballon> ballons = new ArrayList<>();
	private Particle[] particles = null;
	private static final int NUMBER_OF_PARTICLE = 100;
	
	private static final int DELAY = 3000;
	public Canvas(){
		
		setFocusable(true);
		addKeyListener(new TAdapter());
		timer = new Timer(5, this);
		timer2 = new Timer(DELAY, new Action2());
		timer2.start();
		timer.start();
		
	}
	
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		g2d.translate(getWidth()/2, getHeight());
		
		int gHight = gunner.getGunHight();
		int gWidth = gunner.getGunWidth();
		double theta = gunner.getTheta();
		
		double mxPos = gHight*(Math.sin(Math.toRadians(theta)));
		double myPos = gHight*(Math.cos(Math.toRadians(theta)));
		
		gunner.setMxpos(mxPos);
		gunner.setMypos(myPos);
		
		Ellipse2D base = new Ellipse2D.Double(-20,-20,40,40);
		g2d.fill(base);
			
		/*Draw Missile*/
		ArrayList<Missile> ms = gunner.getMissiles();
		for(Missile m:ms){
			Ellipse2D ell = new Ellipse2D.Double(m.getxPos(), -m.getyPos(), m.getRadius() , m.getRadius());
			g2d.fill(ell);
		}
		
		
		/*Draw Balloons*/
		for(Ballon b:ballons){
			Ellipse2D ellb = new Ellipse2D.Double(b.getxPos(),-b.getyPos(),b.getBradius(),b.getBradius());
			if(b.isVisible()){
				g2d.draw(ellb);
			}
			
		}
		//end draw balloons
		
		if(particles!=null){
			for(Particle p:particles){
				Ellipse2D ellp = new Ellipse2D.Double(p.getxPosP(),-p.getyPosP(),p.getRediusP(),p.getRediusP());
				g2d.draw(ellp);
			}
		}
		
		Rectangle2D rect = new Rectangle2D.Double(-gWidth/2,-gHight,gWidth,gHight);
		g2d.rotate(Math.toRadians(theta));
		g2d.draw(rect);
		
		
		g2d.dispose();
		
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		ArrayList<Missile> ms = gunner.getMissiles();
		for(int i=0;i<ms.size();i++){
			Missile m = ms.get(i);
			if(m.isVisible() && (Math.abs(m.getxPos())<=getWidth() || Math.abs(m.getyPos())<=getHeight())){
				m.move();
			}
			else{
				ms.remove(i);
			}
		}
		gunner.rotate();
		
		
		for(int i=0;i<ballons.size();i++){
			Ballon bl = ballons.get(i);
			if(bl.getisBlow()){
				particles = createParticle(bl.getxPos(), bl.getyPos());
				
			}
			if(bl.isVisible() && bl.getyPos()>=0){
				bl.moveB();
			}else{
				ballons.remove(i);
			}
		}
		
		for(int i=0;i<ms.size();i++){
			for(int j=0;j<ballons.size();j++){
				if(ballons.get(j).getBounds().intersects(ms.get(i).getBounds())){
					ms.get(i).setVisible(false);
					ballons.get(j).blow();
				}
			}
		}
		
		if(particles!=null){
			for(Particle p:particles){
				p.moveP();
			}
		}
		
		repaint();
	}
	
	private class TAdapter extends KeyAdapter{
		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			super.keyPressed(arg0);
			gunner.keyPressed(arg0);
		}
		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			super.keyReleased(arg0);
			gunner.keyReleased(arg0);
		}
		
	}
	
	private class Action2 implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			ballons.add(createBallonInRandomPos());
			
		}
		
	}
	
	public Ballon createBallonInRandomPos(){
		Random rand = new Random();
		
		double xPos = rand.nextDouble()*(getWidth()/2);
		if(rand.nextBoolean()){
			xPos = 1*xPos;
		}else{
			xPos = -1*xPos;
		}
		
		double yPos = getHeight()-5;
		//System.out.println(getHeight());
		return new Ballon(xPos, yPos);
		
	}
	
	public Particle[] createParticle(double xPos,double yPos){
		Particle[] particles = new Particle[NUMBER_OF_PARTICLE];
		
		for(int i=0;i<particles.length;i++){
			Random random = new Random();
			double valX = 50*random.nextDouble()+xPos;
			double valY = 50*random.nextDouble()+yPos;
			double veloX = random.nextDouble();
			double veloY = random.nextDouble();
			if(random.nextBoolean()){
				valX = 1*valX;
				valY = 1*valY;
				veloX = 1*veloX;
			}
			else{
				valX = -1*valX;
				valY = -1*valY;
				veloX = -1*veloX;
			}
			particles[i] = new Particle(valX, valY);
			particles[i].setVeloXP(veloX);
			particles[i].setVeloYP(veloY);
		}
		return particles;
	}
}
